set more off   
set scheme s1color
use "$data/descriptive.dta", clear

rename DEN_PROVINCIA provincia
rename DEN_REGIONE regione
sort provIstat year
xtset  provIstat year



*SOUTH=Mezzogiorno of Italy
capture drop South
gen South=1 if regione=="ABRUZZO"
replace South=1 if regione=="BASILICATA"
replace South=1 if regione=="CALABRIA"
replace South=1 if regione=="CAMPANIA"
replace South=1 if regione=="MOLISE"
replace South=1 if regione=="PUGLIA"
replace South=1 if regione=="SARDEGNA"
replace South=1 if regione=="SICILIA"
replace South=0 if South==.

 ****************************************
 
 gen R_tl_av=(R_cl_ori*cl_ori+R_tl_st_ori*st_tl_ori +R_tl_mt_ori*mt_tl_ori+R_tl_lt_ori*lt_tl_ori)/(cl_ori+st_tl_ori+mt_tl_ori+lt_tl_ori)

gen R_cl=R_cl_ori
label variable R_cl "Interest rate credit lines (include fees)"
label variable R_tl_av "Interest rate average on all term loans (include fees)"

gen erate= emp/pop
capture drop GDP_PC
gen GDP_PC=va/pop if year>2009
gen GDP_PW=va/emp

rename va GDP

capture drop _merge
merge m:1 provincia using "$data/latitude.dta"
rename sd_eps_delta_res_pt SD_shock

label variable pop "Total Population, age 15-65"
label variable emp "Total Employment, age 15-65"

egen nfirms=rowtotal(nfirms_age*)
egen dip12=rowtotal(dip_age*)
egen dip_ce_=rowtotal(dip_ce_age*)


egen nfirms_ce_=rowtotal(nfirms_ce_age*)
egen nfirms_ce_ex_ =rowtotal(nfirms_ce_ex_age*)
*FIRM PER CAPITA
gen firm_pc=nfirms/pop
*FIRM PER WORKER
replace firm_pc=nfirms/dip12
gen firm_size=dip12/nfirms
 *Collapse variables 
 gen R_tl_st=R_tl_st_res 
 gen R_tl_mt=R_tl_mt_res
 gen R_tl_lt=R_tl_lt_res
 
gen HICP=.
replace HICP= 1.9  if year==	2021
replace HICP= -0.1 if year==	2020
replace HICP=	0.6	if year==	2019
replace HICP=	1.2	if year==	2018
replace HICP=	1.3	if year==	2017
replace HICP= -0.1	if year==	2016
replace HICP=	0.1	if year==	2015
replace HICP=	0.2	if year==	2014
replace HICP=	1.2	if year==	2013
replace HICP=	3.3	if year==	2012
replace HICP=	2.9	if year==	2011
replace HICP=	1.6	if year==	2010
replace HICP=	0.8	if year==	2009
replace HICP=	3.5	if year==	2008
replace HICP=	2.0	if year==	2007
replace HICP=	2.2	if year==	2006
replace HICP=	2.2	if year==	2005
replace HICP=	2.3	if year==	2004
replace HICP=	2.8	if year==	2003
replace HICP=	2.6	if year==	2002
replace HICP=	2.3	if year==	2001
replace HICP=	2.6	if year==	2000
replace HICP=	1.6	if year==	1999
replace HICP=	1.9	if year==	1998
replace HICP=	1.9	if year==	1997
replace HICP=	4.0	if year==	1996
replace HICP=	5.4	if year==	1995
replace HICP=	4.2	if year==	1994
replace HICP=	4.5	if year==	1993
replace HICP=	4.9	if year==	1992
replace HICP=	6.2	if year==	1991
replace HICP=	6.2	if year==	1990
replace HICP=	6.1	if year==	1989
replace HICP=	4.9	if year==	1988


*egen prova=mean(HICP) if year>=2016 & year<=2020
*egen prova=mean(HICP) if year>=2013 & year<=2017
*egen prova=mean(HICP) if year>=2013 & year<=2015
egen prova=mean(HICP) if year>=2014 & year<=2020
egen HICP3=max(prova) 



gen BCrate_ce=nfirms_ce_age0/pop

*sum nfirms_ce_age0 pop GDP
*des nfirms_ce_age0 pop GDP

rename dip_ce_ dip_ce
rename nfirms_ce_ nfirms_ce
rename nfirms_ce_ex_ nfirms_ce_ex

*keep if year==2015

keep if year>=2007 & year<=2015
*keep if year>=2013 & year<=2015


egen MSD_lndebt_emp_ce_age0=mean(SD_lndebt_emp_ce_age0) 
egen MSD_shock=mean(SD_shock)


/* For panel of balanced firms

*sort provincia year
xtset provIstat year
* edit provincia year dip_cebp_age* nfirms_cebp_age*
gen  growth1=(dip_cebp_age1/L.dip_cebp_age0)*L.nfirms_cebp_age0/nfirms_cebp_age1
gen  growth2=(dip_cebp_age2/L.dip_cebp_age1)*L.nfirms_cebp_age1/nfirms_cebp_age2
gen  growth3=(dip_cebp_age3/L.dip_cebp_age2)*L.nfirms_cebp_age2/nfirms_cebp_age3
gen  growth4=(dip_cebp_age4/L.dip_cebp_age3)*L.nfirms_cebp_age3/nfirms_cebp_age4

gen  growth16=(dip_cebp_age16/L.dip_cebp_age15)*L.nfirms_cebp_age15/nfirms_cebp_age16
sum growth16 if year>=2007 & year<=2015 & South==1
sum growth16 if year>=2007 & year<=2015 & South==0
*/
xtset provIstat year
gen L5dip_cebp_age11=L5.dip_cebp_age11
gen L5nfirms_cebp_age11=L5.nfirms_cebp_age11
gen debt_prova=DEBT_cebp_age16+DEBT_cebp_age17
gen va_prova=va_cebp_age16+va_cebp_age17
foreach var in  dip_cebp_age16 L5dip_cebp_age11 L5nfirms_cebp_age11 nfirms_cebp_age16 debt_prova va_prova {
egen T`var'=total(`var'), by(South year)
egen it_T`var'=total(`var'), by(year)
}

xtset provIstat year
capture drop prova
foreach var in dip_ce_age14 dip_ce_age11 nfirms_ce_age14 nfirms_ce_age11 nfirms_ce_ex_age14 nfirms_ce_ex_age11 {
egen T`var'=total(`var'), by(South year)
egen it_T`var'=total(`var'), by(year)
}

gen Tdip_size_ce_age14=Tdip_ce_age14/(Tnfirms_ce_age14-Tnfirms_ce_ex_age14) if year>=2007 & year<=2015
gen Tdip_size_ce_age11=Tdip_ce_age11/(Tnfirms_ce_age11-Tnfirms_ce_ex_age11) if year>=2007 & year<=2015
gen it_Tdip_size_ce_age14=it_Tdip_ce_age14/(it_Tnfirms_ce_age14-it_Tnfirms_ce_ex_age14) if year>=2007 & year<=2015
gen it_Tdip_size_ce_age11=it_Tdip_ce_age11/(it_Tnfirms_ce_age11-it_Tnfirms_ce_ex_age11) if year>=2007 & year<=2015
*gen  growth16V2=(log(Tdip_size_ce_age14)-log(Tdip_size_ce_age11))/3
*gen it_growth16V2=(log(it_Tdip_size_ce_age14)-log(it_Tdip_size_ce_age11))/3
gen  growth16V2=(Tdip_size_ce_age14/Tdip_size_ce_age11)^(1/3)-1
gen it_growth16V2=(it_Tdip_size_ce_age14/it_Tdip_size_ce_age11)^(1/3)-1
*gen  growth16V2=(Tdip_size_ce_age14/Tdip_size_ce_age11)
*gen it_growth16V2=(it_Tdip_size_ce_age14/it_Tdip_size_ce_age11)


*sum it_growth16V2 growth16V2

*stop

*gen  growth16V2=((Tdip_cebp_age16/TL5dip_cebp_age11)*TL5nfirms_cebp_age11/Tnfirms_cebp_age16-1)/5
*gen it_growth16V2=((it_Tdip_cebp_age16/it_TL5dip_cebp_age11)*it_TL5nfirms_cebp_age11/it_Tnfirms_cebp_age16-1)/5

gen leverage16V2=Tdebt_prova/Tva_prova
gen it_leverage16V2=it_Tdebt_prova/it_Tva_prova

sum growth16V2 if year>=2007 & year<=2015 & South==1
sum growth16V2 if year>=2007 & year<=2015 & South==0
*label variable growth16V2 "Growth rate incumbent firm age 12-14"
*label variable leverage16V2 "Leverage ratio incumbent firm age 11-16"
*gen debt_prova=DEBT_ce_age11+DEBT_ce_age12+DEBT_ce_age13+DEBT_ce_age14+DEBT_ce_age15+ DEBT_ce_age16
label variable growth16V2 "Relative employment size, 11-14 yrs"
label variable leverage16V2 "Leverage ratio incumbent firm age 11-16"

 sum growth16V2 it_growth16V2 leverage16V2 it_leverage16V2
 
 replace GDP=. if year==2007 | year==2008

keep sigla latitude provIstat provincia regione regione South year HICP3  ///
g_mt_lt_tl_res mt_lt_tl_res g_lt_tl_ori g_st_tl_res g_mt_lt_tl_res  g_cl_res st_tl_res mt_lt_tl_res  cl_res R_tl_lt_res ///
R_cl_ori cl_ori R_tl_st_ori st_tl_ori  R_tl_mt_ori mt_tl_ori R_tl_lt_ori lt_tl_or cl_ori st_tl_ori mt_tl_ori lt_tl_ori /// 
 firm_pc firm_size  BCrate_ce  SD_shock MSD_shock ///
 nfirms_ce_ex nfirms_ce dip_ce pop emp GDP  ///
   nfirms_age0 nfirms_age1 nfirms_age2 nfirms_age3 nfirms_age4 nfirms_age5 nfirms_age6 nfirms_age7 nfirms_age8 nfirms_age9 nfirms_age10 nfirms_age11 nfirms_age12 nfirms_age13 nfirms_age14 nfirms_age15 nfirms_age16 nfirms_age17 ///
  nfirms_ex_age0 nfirms_ex_age1 nfirms_ex_age2 nfirms_ex_age3 nfirms_ex_age4 nfirms_ex_age5 nfirms_ex_age6 nfirms_ex_age7 nfirms_ex_age8 nfirms_ex_age9 nfirms_ex_age10 nfirms_ex_age11 nfirms_ex_age12 nfirms_ex_age13 nfirms_ex_age14 nfirms_ex_age15 nfirms_ex_age16  nfirms_ex_age17 ///
  nfirms_pg_age0 nfirms_pg_age1 nfirms_pg_age2 nfirms_pg_age3 nfirms_pg_age4 nfirms_pg_age5 nfirms_pg_age6 nfirms_pg_age7 nfirms_pg_age8 nfirms_pg_age9 nfirms_pg_age10 nfirms_pg_age11 nfirms_pg_age12 nfirms_pg_age13 nfirms_pg_age14 nfirms_pg_age15 nfirms_pg_age16 nfirms_pg_age17 ///
  nfirms_pg_ex_age0 nfirms_pg_ex_age1 nfirms_pg_ex_age2 nfirms_pg_ex_age3 nfirms_pg_ex_age4 nfirms_pg_ex_age5 nfirms_pg_ex_age6 nfirms_pg_ex_age7 nfirms_pg_ex_age8 nfirms_pg_ex_age9 nfirms_pg_ex_age10 nfirms_pg_ex_age11 nfirms_pg_ex_age12 nfirms_pg_ex_age13 nfirms_pg_ex_age14 nfirms_pg_ex_age15  nfirms_pg_ex_age16 nfirms_pg_ex_age17 ///
 nfirms_pg_ins_age0 nfirms_pg_ins_age1 nfirms_pg_ins_age2 nfirms_pg_ins_age3 nfirms_pg_ins_age4 nfirms_pg_ins_age5 nfirms_pg_ins_age6 nfirms_pg_ins_age7 nfirms_pg_ins_age8 nfirms_pg_ins_age9 nfirms_pg_ins_age10 nfirms_pg_ins_age11 nfirms_pg_ins_age12 nfirms_pg_ins_age13 nfirms_pg_ins_age14 nfirms_pg_ins_age15 nfirms_pg_ins_age16 nfirms_pg_ins_age17 ///
 nfirms_ce_age0     nfirms_ce_age1     nfirms_ce_age2     nfirms_ce_age3     nfirms_ce_age4     nfirms_ce_age5     nfirms_ce_age6     nfirms_ce_age7     nfirms_ce_age8     nfirms_ce_age9     nfirms_ce_age10     nfirms_ce_age11     nfirms_ce_age12     nfirms_ce_age13     nfirms_ce_age14     nfirms_ce_age15     nfirms_ce_age16     nfirms_ce_age17 ///
 nfirms_ce_ex_age0  nfirms_ce_ex_age1  nfirms_ce_ex_age2  nfirms_ce_ex_age3  nfirms_ce_ex_age4  nfirms_ce_ex_age5  nfirms_ce_ex_age6  nfirms_ce_ex_age7  nfirms_ce_ex_age8  nfirms_ce_ex_age9  nfirms_ce_ex_age10  nfirms_ce_ex_age11  nfirms_ce_ex_age12  nfirms_ce_ex_age13  nfirms_ce_ex_age14  nfirms_ce_ex_age15  nfirms_ce_ex_age16  nfirms_ce_ex_age17 ///
nfirms_ce_ins_age0 nfirms_ce_ins_age1 nfirms_ce_ins_age2 nfirms_ce_ins_age3 nfirms_ce_ins_age4 nfirms_ce_ins_age5 nfirms_ce_ins_age6 nfirms_ce_ins_age7 nfirms_ce_ins_age8 nfirms_ce_ins_age9 nfirms_ce_ins_age10 nfirms_ce_ins_age11 nfirms_ce_ins_age12 nfirms_ce_ins_age13 nfirms_ce_ins_age14 nfirms_ce_ins_age15 nfirms_ce_ins_age16 nfirms_ce_ins_age17 ///
dip_ce_age0        dip_ce_age1        dip_ce_age2        dip_ce_age3        dip_ce_age4        dip_ce_age5        dip_ce_age6        dip_ce_age7        dip_ce_age8        dip_ce_age9        dip_ce_age10        dip_ce_age11        dip_ce_age12        dip_ce_age13        dip_ce_age14        dip_ce_age15        dip_ce_age16        dip_ce_age17 ///
debt_ce_age0       debt_ce_age1       debt_ce_age2       debt_ce_age3       debt_ce_age4       debt_ce_age5       debt_ce_age6       debt_ce_age7       debt_ce_age8       debt_ce_age9       debt_ce_age10       debt_ce_age11       debt_ce_age12       debt_ce_age13       debt_ce_age14       debt_ce_age15       debt_ce_age16       debt_ce_age17 ///
va_ce_age0         va_ce_age1         va_ce_age2         va_ce_age3         va_ce_age4         va_ce_age5         va_ce_age6         va_ce_age7         va_ce_age8         va_ce_age9         va_ce_age10         va_ce_age11         va_ce_age12         va_ce_age13         va_ce_age14         va_ce_age15         va_ce_age16         va_ce_age17 ///
DEBT_ce_age0       DEBT_ce_age1       DEBT_ce_age2       DEBT_ce_age3       DEBT_ce_age4       DEBT_ce_age5       DEBT_ce_age6       DEBT_ce_age7       DEBT_ce_age8       DEBT_ce_age9       DEBT_ce_age10       DEBT_ce_age11       DEBT_ce_age12       DEBT_ce_age13       DEBT_ce_age14       DEBT_ce_age15       DEBT_ce_age16       DEBT_ce_age17 ///
R_ce_age0          R_ce_age1          R_ce_age2          R_ce_age3          R_ce_age4          R_ce_age5          R_ce_age6          R_ce_age7          R_ce_age8          R_ce_age9          R_ce_age10          R_ce_age11          R_ce_age12          R_ce_age13          R_ce_age14          R_ce_age15          R_ce_age16          R_ce_age17           ///
R_cl_ce_age0       R_cl_ce_age1       R_cl_ce_age2       R_cl_ce_age3       R_cl_ce_age4       R_cl_ce_age5       R_cl_ce_age6       R_cl_ce_age7       R_cl_ce_age8       R_cl_ce_age9       R_cl_ce_age10       R_cl_ce_age11       R_cl_ce_age12       R_cl_ce_age13       R_cl_ce_age14       R_cl_ce_age15       R_cl_ce_age16       R_cl_ce_age17         ///
SD_lnemp_ce_age0 SD_lndebt_emp_ce_age0 SD_lnemp_ce_age0 MSD_lndebt_emp_ce_age0 ///
growth16V2 it_growth16V2 leverage16V2 it_leverage16V2

label variable regione "Region code"
label variable South "South dummy"
label variable year "Year"
label variable firm_pc "N. firms per capita"
label variable  firm_size "Average firm size (employment)"
label variable  HICP3 "Inflation rate"
label variable  BCrate_ce "Business creation rate, CERVED"
label variable MSD_lndebt_emp_ce_age0  "SD log debt per employee at entry"
label variable MSD_shock "SD idiosyncratic shock"
*label variable it_growth16V2 "Growth rate incumbent firm age 12-14, Italy"
label variable it_growth16V2 "Relative employment size, 11-14 yrs, Italy"
label variable it_leverage16V2 "Leverage ratio age 16, Italy" 

save "$data/calibrationdataset.dta", replace

use "$data/descriptive.dta", clear
*Divide by total number of hours worked in  ayear (maximum). Allow for 4 weeks of compulsory holidays
 rename DEN_PROVINCIA provincia
 rename DEN_REGIONE regione
 
 egen popM=mean(pop), by(provincia)
 *capture drop South
 gen South2=1 if regione=="ABRUZZO"
 replace South2=1 if regione=="BASILICATA"
 replace South2=1 if regione=="CALABRIA"
 replace South2=1 if regione=="CAMPANIA"
 replace South2=1 if regione=="MOLISE"
 replace South2=1 if regione=="PUGLIA"
 replace South2=1 if regione=="SARDEGNA"
 replace South2=1 if regione=="SICILIA"
 replace South2=0 if South2==.
 *cratere sismico del Centro Italia
 /*
 replace South2=1 if provincia=="L'AQUILA"
 replace South2=1 if provincia=="TERAMO"
 replace South2=1 if provincia=="RIETI"
 replace South2=1 if provincia=="MACERATA"
 replace South2=1 if provincia=="ASCOLI PICENO"
 replace South2=1 if provincia=="PERUGIA"
 replace South2=1 if provincia=="TERNI"
 */
 *replace South=South2

 label variable South "South dummy"
label variable year "Year"
 
 
 **************************************************
 * RESTO AL SUD APPLIES to all firms in South of Italy plus following provinces
* Enterpreneurs and start-ups should be created after 21/06/2017
* So First treatment year is 2017 
 *drop CENTRO
 
 *drop if  AREA_GEO ==3
 
 
 /*
 La  lista completa dei 140 Comuni colpiti e danneggiati dal sisma in Centro Italia cui spetteranno gli aiuti ed i rimborsi previsti dal decreto legge terremoto. 

L’elenco dei Comuni 140 comuni divisi per regione:

Abruzzo
Barete (Aq); Cagnano Amiterno (Aq); Campli (TE) Campotosto (AQ); Capitignano (AQ); Castelcastagna (Te); Castelli (TE); Civitella del Tronto (TE);  Colledara (Te); Cortino (TE); Crognaleto (TE); Fano Adriano (Te). Farindola (Pe); Isola del Gran Sasso (Te); Montereale (AQ); Montorio al Vomano (TE); Pietracamela (Te) Pizzoli (Aq); Rocca Santa Maria (TE); Teramo; Torricella Sicura (TE); Tossicia (TE); Valle Castellana (TE).

Lazio
Accumoli (RI); Amatrice (RI); Antrodoco (RI); Borbona (RI); Borgo Velino (RI); Cantalice (RI); Castel Sant’Angelo (RI); Cittaducale (RI); Cittareale (RI); Leonessa (RI); Micigliano (RI); Poggio Bustone (RI) Posta (RI); Rieti;  Rivodutri (RI).

Marche
Acquacanina (MC); Acquasanta Terme (AP); Amandola (FM); Apiro (MC); Appignano del Tronto (AP);
Arquata del Tronto (AP); Ascoli Piceno; Belforte del Chienti (MC); Belmonte Piceno (FM); Bolognola (MC);
Caldarola (MC); Camerino (MC); Camporotondo di Fiastrone (MC); Castel di Lama (AP); Castelraimondo (MC);
Castelsantangelo sul Nera (MC); Castignano (AP); Castorano (AP); Cerreto D’esi (AN); Cessapalombo (MC);
Cingoli (MC); Colli del Tronto (AP); Colmurano (MC); Comunanza (AP); Corridonia (MC); Cossignano (AP);
Esanatoglia (MC); Fabriano (AN); Falerone (FM); Fiastra (MC); Fiordimonte (MC); Fiuminata (MC);
Folignano (AP); Force (AP); Gagliole (MC); Gualdo (MC); Loro Piceno (MC);  Macerata; Maltignano (AP);
Massa Fermana (FM); Matelica (MC); Mogliano (MC); Monsapietro Morico (FM); Montalto delle Marche (AP);
Montappone (FM); Monte Rinaldo (FM); Monte San Martino (MC); Monte Vidon Corrado (FM);
Montecavallo (MC); Montedinove (AP);  Montefalcone Appennino (FM); Montefortino (FM); Montegallo (AP);
Montegiorgio (FM); Monteleone (FM); Montelparo (FM); Montemonaco (AP);  Muccia (MC); Offida (AP);
Ortezzano (FM); Palmiano (AP); Penna San Giovanni (MC); Petriolo (MC); Pieve Torina (MC);
Pievebovigliana (MC);  Pioraco (MC); Poggio San Vicino (MC);  Pollenza (MC); Ripe San Ginesio (MC);
Roccafluvione (AP); Rotella (AP); San Ginesio (MC); San Severino Marche (MC); Santa Vittoria in Matenano (FM);
Sant’Angelo in Pontano (MC); Sarnano (MC); Sefro (MC); Serrapetrona (MC); Serravalle del Chienti (MC);
Servigliano (FM); Smerillo (FM); Tolentino (MC); Treia (MC); Urbisaglia (MC); Ussita (MC); Venarotta (AP);
Visso (MC).

Umbria
Arrone (TR); Cascia (PG); Cerreto di Spoleto (PG); Ferentillo (TR); Montefranco (TR); Monteleone di Spoleto (PG); Norcia (PG); Poggiodomo (PG); Polino (TR); Preci (PG); Sant’Anatolia di Narco (PG); Scheggino (PG); Sellano (PG); Spoleto (PG); Vallo di Nera (PG).
*/ 
 
*stop
tab South South2

sort provIstat year
sort provincia year
//gen urate=une/(une+ emp)

* RAS: all firms i cui soci con meno di x anni detengono almeno il 66% del capitale, non sono soci in altre imprese, non operano nei settori ateco 1, 2, 45, 46, 47, 68, 69, 84, 97, 98, 99
* old_RAS: all firms i cui soci hanno meno di x anni



*global Cabsorb "i.provaC#i.age i.year"




foreach num in 0 1 2 3 4 5 6 7 8 9 10 11 {
//drop RAS_55_debfin_age`num'
rename old_RAS_55_debfin_age`num' RAS_55_debfin_age`num'
//drop RAS_55_va_age`num'
rename old_RAS_55_va_age`num' RAS_55_va_age`num'
//drop RAS_55_dipxva_age`num'
rename old_RAS_55_dipxva_age`num' RAS_55_dipxva_age`num'
//drop RAS_55_nfirms_ex_age`num'
rename old_RAS_55_nfirms_ex_age`num' RAS_55_nfirms_ex_age`num'
//drop RAS_55_nfirms_age`num'
rename old_RAS_55_nfirms_age`num' RAS_55_nfirms_age`num'
//drop RAS_55_dip_age`num'
rename old_RAS_55_dip_age`num' RAS_55_dip_age`num'
}


keep provincia year South RAS_55_debfin_age* RAS_55_va_age* RAS_55_dipxva_age* RAS_55_nfirms_ex_age* RAS_55_nfirms_age* RAS_55_dip_age*

save "$data/EffectSubsidydataset.dta", replace

****************************************************
*EVENT STUDY DATA SET

*global input  $dir/input                 
*global output $dir/output


*global lb -12
global ub 12
*use $input/event_study_dataset.dta, clear


use $data/event_study_dataset.dta, clear

*label variable _totrate "Number of reimbursements received"
rename totrate _totrate
bys id: egen totrate=mean(_totrate)
*** select firms above CR threshold
keep if FinBancario>=30000  

label variable totrate "Number of reimbursements received"
label variable ordrimb "1st or 2nd reimbursement"
label variable ym "Current month"
label variable reimbursement "Amount reimbursed in the month"
label variable id "Firm ID"
label variabl dip "Total number of employees"
label variable bankdebt "Outstanding bank debt"
//label variable ym_accRSUD "Month of approval of I Stay in the South"
//label variable ym_nascita "Month of birth of firm (infocamere)"
//label variable ym_morte  "Month of death of firm (infocamere)"
drop _totrate FinBancario  med_montesalari


save $data/datasetRgressionReimburse.dta, replace

use $data/datasetRgressionReimburse.dta, clear

//gen codfisc_mef=id 
gen _ym_1st=ym if ordrimb==1
bys id: egen ym_1st=mean(_ym_1st)
gen diff_1st=ym-ym_1st


*** define 1st and 2nd subsidy events 
keep if diff_1st>-18
drop if diff_1st<18 & totrat==1 
gen _ym_2nd=ym if ordrimb==2
bys id: egen ym_2nd=mean(_ym_2nd)

*Here I impose that firms in the sample received TWO REIMBURSEMENTS
gen diff_2nd=ym-ym_2nd
keep if diff_2nd<18 



*** select CERVED firms
//merge m:1 codfisc_mef using $input/cerved_codfisc, keep(3) 





*** reimbursements for each event 
sort codfisc ym
replace reimbursement=0 if reimbursement==.        
gen _NLR1=reimbursement if  ordrimb==1
bys codfisc_mef: egen NLR1=mean(_NLR1)
gen _NLR2=reimbursement if ordrimb==2
bys codfisc_mef: egen NLR2=mean(_NLR2)






xtset id ym


**** check on duration since approval
gen _ym_NLR1=ym if ordrimb==1
gen _ym_NLR2=ym if  ordrimb==2
gen dur_NLR1=_ym_NLR1-ym_accRSUD if reimbursement>0  // 1st reimbursement in months since approval
gen dur_NLR2=_ym_NLR2-ym_accRSUD if reimbursement>0   //2nd reimbursment in months since approval
su dur_*, d




drop _* 

gen age=ym-ym_birth  //ym_accRSUD
gen age2=age*age 



*** event dummies
gen NLR1time0=( ordrimb==1)
gen NLR2time0=( ordrimb==2)
*gen exantime0=(ym_accRSUD==ym)
 
sort id ym
foreach x of numlist 1/$ub {
*	gen exantimem`x'=(F`x'.exantime0==1)
*	gen exantimep`x'=(L`x'.exantime0==1)
	gen NLR1timem`x'=(F`x'.NLR1time0==1)
	gen NLR1timep`x'=(L`x'.NLR1time0==1)
	gen NLR2timem`x'=(F`x'.NLR2time0==1)
	gen NLR2timep`x'=(L`x'.NLR2time0==1)
	
	
	
}


*gen exantimem_res=0
*gen exantimep_res=0

gen NLR1timem_res=0
gen NLR1timep_res= 0 
gen NLR2timem_res=0
gen NLR2timep_res= 0 


foreach x of numlist 1/78 {   // =764-678 (max interval)
	local ubp1= $ub +`x'
*	replace exantimem_res=1 if  F`ubp1'.exantime0==1
*	replace exantimep_res=1 if  L`ubp1'.exantime0==1
	
	replace NLR1timem_res=1 if  F`ubp1'.NLR1time0==1
	replace NLR1timep_res=1 if  L`ubp1'.NLR1time0==1
	replace NLR2timem_res=1 if  F`ubp1'.NLR2time0==1
	replace NLR2timep_res=1 if  L`ubp1'.NLR2time0==1
	
}




*** replace variables
gen EX0= NLR2time0 * NLR2 + NLR1time0 * NLR1
replace NLR2time0 =NLR2time0 * NLR2
replace NLR1time0  = NLR1time0 * NLR1
foreach x of numlist 1/$ub {
	
	replace NLR1timem`x'=NLR1timem`x' * NLR1
	replace NLR1timep`x'= NLR1timep`x' * NLR1 
	replace NLR2timem`x'=NLR2timem`x' * NLR2
	replace NLR2timep`x'= NLR2timep`x' * NLR2
	egen EXm`x'=rowtotal(NLR2timem`x' NLR1timem`x')
	egen EXp`x'=rowtotal(NLR2timep`x' NLR1timep`x')
	
}

foreach x in  NLR1 NLR2{
	replace `x'timem_res=`x'timem_res * `x' 
	replace `x'timep_res= `x'timep_res * `x'
}
egen EXm_res=rowtotal(NLR2timem_res NLR1timem_res)
	egen EXp_res=rowtotal(NLR2timep_res NLR1timep_res)
*************/

	

******************************************************
keep if ym>=ym_birth & ym<=ym_death // infocamere
//keep if ym>=ym_birth & ym<=ym_death  // uninps
**************************************************
*replace diff_1st=diff_1st+17
*replace diff_2nd=diff_2nd+61
gen dummy_1st=diff_1st>=0
gen dummy_2nd=diff_2nd>=0


*** choose variables 
*global vary1  bankdebt   //bankdebt  employment   
*global varx1   EXm_res EXm12 EXm11 EXm10 EXm9 EXm8 EXm7 EXm6 EXm5 EXm4 EXm3 EXm2 EXm1 EX0 EXp* dummy_1st dummy_2nd 
sort id ym
tsset id ym
*gen employment=dip  * 10^3
*gen employment=dip  * 10^4
gen employment=dip

**** normalization
*replace exantimem1=0
 
*replace EXm_res=0

drop if bankdebt==0
by id: egen Memployment=mean(employment)
drop if Memployment==0

********************************************
* TOTAL REIMBURSEMENT
gen subsi_post=EXp_res+EXp12+EXp11+ EXp10+ EXp9+ EXp8+ EXp7+ EXp6+ EXp5+ EXp4+ EXp3+ EXp2+ EXp1 + EX0
*gen subsi_post_1st=NLR1timep_res+NLR1timep12+NLR1timep11+ NLR1timep10+ NLR1timep9+ NLR1timep8+ NLR1timep7+ NLR1timep6+ NLR1timep5+ NLR1timep4+ NLR1timep3+ NLR1timep2+ NLR1timep1 + NLR1time0
*gen subsi_post_2nd=NLR2timep_res+NLR2timep12+NLR2timep11+ NLR2timep10+ NLR2timep9+ NLR2timep8+ NLR2timep7+ NLR2timep6+ NLR2timep5+ NLR2timep4+ NLR2timep3+ NLR2timep2+ NLR2timep1 + NLR2time0 
gen  EXp3_res=EXp_res+EXp12+EXp11+ EXp10+ EXp9+ EXp8+ EXp7+ EXp6+ EXp5+ EXp4+ EXp3
gen  EXp2_res=EXp_res+EXp12+EXp11+ EXp10+ EXp9+ EXp8+ EXp7+ EXp6+ EXp5+ EXp4+ EXp3+ EXp2


foreach var in  EX0  EXp1 EXp2 EXp3_res bankdebt subsi_post reimbursement  { 
replace `var' =`var'/10000
}
*Table
label variable bankdebt "Bank debt"
label variable employment  "Employment"
label variable subsi_post "Amount reimbursed, $10^4$\euro "
*label variable subsi_post_1st "First reimbursement"
*label variable subsi_post_2nd "Second reimbursement"
label variable EX0 "Month of reimbursement"  
label variable EXp1 "1st month after reimburs." 
label variable EXp2 "2nd month after reimburs."
*label variable EXp3 "Third month after reimbursement"
label variable EXp3_res "More than 2 months after reimburs."
label variable EXp2_res "More than 1 month after reimburs."
* gen employment_perce=employment/totdip

rename ym_birth ym_nascita 
rename ym_death ym_morte
keep bankdebt employment subsi_post EX0 EXp1 EXp2 EXp3_res EXp2_res ym dip ym_morte ym_nascita reimbursement ordrimb ym_accRSUD id totrate  dummy_1st dummy_2nd codfisc
egen  prova=group(codfisc_mef) 

label variable codfisc_mef "Firm id"
*label variable date "Date"
*label variable age "Firm age"
label variable dummy_1st "Dummy for first ISS payment" 
label variable dummy_2nd "Dummy for second ISS payment"
save "$data/Table3_dataset.dta", replace
